﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arduino.ControlPanel
{
    public partial class ucPinDigital : UserControl, IUcPin
    {
        private Firmata.Types.Pins mPin;
        private Firmata.Firmata mFirmata;
        private bool mReportLocal = false;
        private bool mModeLocal = true;

        public ucPinDigital(Firmata.Types.Pins Pin, Firmata.Firmata _Firmata)
        {
            InitializeComponent();
            mPin = Pin;
            mFirmata = _Firmata;

            groupBox.Text = Pin.ToString();
            try
            {
                comboBoxMode.Enabled = false;
                comboBoxMode.DataSource = Enum.GetValues(typeof(Firmata.Types.PinMode));
                comboBoxMode.SelectedItem = (int)Firmata.Types.PinMode.None;
            }
            catch (Exception)
            { }
        }

        public Firmata.Types.Pins GetPin()
        {
            return mPin;
        }

        public void RefreshPin()
        {
            if (mFirmata != null)
            {
                comboBoxMode.SelectedItem = mFirmata.getPinMode(mPin);
                checkBoxReporting.Checked = mFirmata.getPinReportMode(mPin);

                if (IsSimulation())
                {
                    switch (mFirmata.getPinMode(mPin))
                    {
                        case Firmata.Types.PinMode.Input:
                        case Firmata.Types.PinMode.Output:
                            panelValue.Visible = true;
                            groupBoxSimulation.Enabled = true;
                            labelValue.Text = ((Firmata.Types.PinDigitalValue)mFirmata.digitalRead(mPin)).ToString();
                            trackBar1.Maximum = 1;
                            break;

                        case Firmata.Types.PinMode.PWM:
                            panelValue.Visible = true;
                            groupBoxSimulation.Enabled = true;
                            labelValue.Text = mFirmata.digitalRead(mPin).ToString();
                            trackBar1.Maximum = 255;
                            break;

                        default:
                            panelValue.Visible = false;
                            groupBoxSimulation.Enabled = false;
                            break;
                    }
                }
                else
                {
                    switch (mFirmata.getPinMode(mPin))
                    {
                        case Firmata.Types.PinMode.Input:
                            panelValue.Visible = true;
                            labelValue.Text = ((Firmata.Types.PinDigitalValue)mFirmata.digitalRead(mPin)).ToString();
                            break;

                        default:
                            panelValue.Visible = false;
                            break;

                    }
                }
            }
        }

        private void comboBoxMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mFirmata != null && !mModeLocal)
                mFirmata.setPinMode(mPin, (Firmata.Types.PinMode)comboBoxMode.SelectedItem);
            mModeLocal = false;
        }

        private bool IsSimulation()
        {
            try
            {
                return checkBoxSimulation.Checked;
            }
            catch (Exception) { }
            return false;
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (mFirmata != null)
            {
                switch ((Firmata.Types.PinMode)comboBoxMode.SelectedItem)
                {
                    case Firmata.Types.PinMode.Input:
                        break;

                    case Firmata.Types.PinMode.Output:
                        mFirmata.digitalWrite(mPin, (Firmata.Types.PinDigitalValue)trackBar1.Value);
                        break;

                    case Firmata.Types.PinMode.PWM:
                        mFirmata.analogWrite(mPin, trackBar1.Value);
                        break;


                    default:
                        break;
                }
                RefreshPin();
            }
        }

        private void checkBoxSimulation_CheckedChanged(object sender, EventArgs e)
        {
            if (mFirmata != null)
            {
                groupBoxSimulation.Visible = IsSimulation();
                comboBoxMode.Enabled = IsSimulation();
                if (IsSimulation() && mFirmata.pinsValues.ContainsKey(mPin)) trackBar1.Value = mFirmata.pinsValues[mPin];
            }
        }

        private void groupBoxSimulation_EnabledChanged(object sender, EventArgs e)
        {
            foreach (Control control in groupBoxSimulation.Controls)
                control.Enabled = this.groupBoxSimulation.Enabled;
        }

        private void checkBoxReporting_CheckedChanged(object sender, EventArgs e)
        {
            if (mFirmata != null && !mReportLocal)
                mFirmata.sendSetPinReporting(mPin, checkBoxReporting.Checked);
            mReportLocal = false;
        }

    }
}
